package com.lhy.admin.common.mvcplus.wrapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lhy.admin.common.mvcplus.entity.ComparatorEnum;
import org.jetbrains.annotations.Nullable;
import org.springframework.util.Assert;
/**
 * JSONObjectEqComparator
 *
 * @author hyluan
 * @date 2022年11月21日 14:11
 */
public class JSONObjectEqComparator extends AbstractJSONObjectComparator {

    public static final String JSON_EQ_SQL_TEMP = "JSON_EXTRACT(%s,'$.%s') = {0}";

    @Override
    @Nullable
    public Class getAttrFieldClass(QueryWrapper queryWrapper, String[] name) {
        Assert.isTrue(name.length == 2, "name长度不为2");
        return String.class;
    }

    public static String sql(String colName, String attrName) {
        return JSON_EQ_SQL_TEMP.formatted(colName, attrName);
    }

    @Override
    public String expressionSql(String colName, String attrName) {
        return sql(colName,attrName);
    }

    @Override
    public ComparatorEnum comparatorEnum() {
        return ComparatorEnum.JSON_OBJECT_EQ;
    }
}
